/* 
 * File:   Cube.cpp
 * Author: Gab'z
 * 
 * Created on 4 juin 2012, 09:40
 */

#include "Cube.h"
#include "../../../Context.h"
#include "../../literal/Stack.h"

namespace calculator {

    Cube::Cube(const std::string name) : UnaryOperator(name) {
    }

    Cube::Cube(const Cube& orig) : UnaryOperator(orig._name) {
    }

    Cube::~Cube() {
    }

    std::string Cube::isExecutable() const {
        if (Context::getInstance()->getCurrentStack()->size() == 0) return "Empty stack";
        return "";
    }

    Cube* Cube::clone() const {
        return new Cube(_name);
    }

    const Number* Cube::apply(const Number* number) const throw (ArithmeticException) {
        const Number* n1;
        const Number* n2;
        try {
            n1 = mul(number, number);
        } catch (ArithmeticException& e) {
            throw;
        }
        try {
            n2 = mul(number, n1);
        } catch (ArithmeticException& e) {
            delete n1;
            throw;
        }
        delete n1;
        return n2;
    }
}